<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/5/25
 * Time: 9:04
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;
use fast\Pinyin;

class Secure extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';


    //司机列表
    public function driver()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $driver = Db::name('driver')->select();
        if($driver)
        {
            $this->success('',$driver);
        }
        $this->error('暂无数据');
    }

    //司机添加
    public function driver_add()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }
        $params = input('post.');
        $result = $this->validate($params,'Driver');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $time = time();
        $params['createtime'] = $time;
        $params['updatetime'] = $time;
        $res = Db::name('driver')->insert($params);
        if($res)
        {
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    //司机详情
    public function driver_detail()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }

        $param = input('get.');
        if(!isset($param['id']) || $param['id']=='')
        {
            $this->error('缺少参数');
        }

        $info = Db::name('driver')->where('id',$param['id'])->find();
        if($info)
        {
            $this->success('',$info);
        }
        $this->error('暂无数据');
    }

    //司机删除
    public function driver_del()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }

        $param = input('get.');
        if(!isset($param['id']) || $param['id']=='')
        {
            $this->error('缺少参数');
        }
        $res = Db::name('driver')->where('id',$param['id'])->delete();
        if($res)
        {
            $this->success('删除成功');
        }
        $this->error('删除失败');

    }

    //司机修改
    public function driver_save()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }

        $params = input('post.');
        if(!isset($params['id']) || $params['id']=='')
        {
            $this->error('缺少参数');
        }
        $result = $this->validate($params,'Driver');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $id = $params['id'];
        unset($params['id']);
        $res = Db::name('driver')->where('id',$id)->update($params);
        if($res)
        {
            $this->success('修改成功');
        }
        $this->error('修改失败');

    }

    //---------------------------------------------------------------------------
    //校车列表
    public function car()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $car = Db::name('car')->alias('a')
                ->join('fa_driver b','a.driver_id=b.id','left')
                ->field('a.id,a.license_plate,a.IMEI,b.name')
                ->select();
        if($car)
        {
            $this->success('',$car);
        }
        $this->error('暂无数据');
    }

    //校车添加
    public function car_add()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }
        $params = input('post.');

        $result = $this->validate($params,'Car');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $pinyin = strtolower(Pinyin::get($params['license_plate'])) ;
        $table_name = config('database.prefix').'car_'.$pinyin;

        $isTable=db()->query('SHOW TABLES LIKE '."'".$table_name."'");
        if($isTable){
            //表存在

        }else {
            //表不存在
            //echo 2;
            $sql = " CREATE TABLE IF NOT EXISTS `$table_name` (
		`id` int(11) NOT NULL AUTO_INCREMENT,
        `IMEI` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '校车终端国际移动设备识别码',
        `CSQ` char(2) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '信号强度0-31',
        `GPS_LNG` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '经度,WGS-84坐标系',
        `GPS_LAT` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '纬度,WGS-84坐标系',
        `SPEED` double DEFAULT NULL COMMENT '速度,来源GPS模块检测,单位km/h',
        `SYS_VER` char(6) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '设备软件版本号',
        `TEMP` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '温度,单位℃',
        `HUM` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '湿度',
        `INIT` tinyint(1) DEFAULT '1' COMMENT '0-服务端下发获取后上报,1-定时主动上报',
        `TIME` int(11) DEFAULT NULL COMMENT '数据采集后上报时的UNIX时间戳',
        `DTIME` date DEFAULT NULL,
        PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='汽车实时位置';";
            Db::startTrans();
            try{
                Db::execute($sql);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('数据表创建失败:'.$e->getMessage());
            }

        }
        $params['pinyin'] = $pinyin;
        $time = time();
        $params['createtime'] = $time;
        $params['updatetime'] = $time;
        $res = Db::name('car')->insert($params);
        if($res)
        {
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    //校车详情
    public function car_detail()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }

        $param = input('get.');
        if(!isset($param['id']) || $param['id']=='')
        {
            $this->error('缺少参数');
        }

        $info = Db::name('car')->where('id',$param['id'])->find();
        if($info)
        {
            $this->success('',$info);
        }
        $this->error('暂无数据');
    }

    //校车删除
    public function car_del()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }

        $param = input('get.');
        if(!isset($param['id']) || $param['id']=='')
        {
            $this->error('缺少参数');
        }
        $res = Db::name('car')->where('id',$param['id'])->delete();
        if($res)
        {
            $this->success('删除成功');
        }
        $this->error('删除失败');

    }

    //校车修改
    public function car_save()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }

        $params = input('post.');
        if(!isset($params['id']) || $params['id']=='')
        {
            $this->error('缺少参数');
        }
        $result = $this->validate($params,'Car');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $pinyin =strtolower(Pinyin::get($params['license_plate'])) ;
        $table_name = config('database.prefix').'car_'.$pinyin;


        $isTable=db()->query('SHOW TABLES LIKE '."'".$table_name."'");
        if($isTable){
            //表存在

        }else {
            //表不存在
            //echo 2;
            $sql = " CREATE TABLE IF NOT EXISTS `$table_name` (
		`id` int(11) NOT NULL AUTO_INCREMENT,
        `IMEI` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '校车终端国际移动设备识别码',
        `CSQ` char(2) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '信号强度0-31',
        `GPS_LNG` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '经度,WGS-84坐标系',
        `GPS_LAT` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '纬度,WGS-84坐标系',
        `SPEED` double DEFAULT NULL COMMENT '速度,来源GPS模块检测,单位km/h',
        `SYS_VER` char(6) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '设备软件版本号',
        `TEMP` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '温度,单位℃',
        `HUM` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT '湿度',
        `INIT` tinyint(1) DEFAULT '1' COMMENT '0-服务端下发获取后上报,1-定时主动上报',
        `TIME` int(11) DEFAULT NULL COMMENT '数据采集后上报时的UNIX时间戳',
        `DTIME` date DEFAULT NULL,
        PRIMARY KEY (`id`)
		) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='汽车实时位置';";
            Db::startTrans();
            try{
                Db::execute($sql);
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('数据表创建失败:'.$e->getMessage());
            }

        }
        $params['pinyin'] = $pinyin;
        $id = $params['id'];
        unset($params['id']);
        $res = Db::name('car')->where('id',$id)->update($params);
        if($res)
        {
            $this->success('修改成功');
        }
        $this->error('修改失败');

    }

    //异常消息
    public function abnormal()
    {
        if (!Request::instance()->isGet())
        {
            $this->error('请求方式错误');
        }
        $params = input('get.');

        $list = Db::name('abnormal_information')->field('id,license_plate,state,content,from_unixtime(createtime) as createtime')->paginate($params['listRows'],false,['page'=>$params['page']])->toArray();

        if($list)
        {
            $this->success('',$list);
        }
        else
        {
            $this->error('暂无数据');
        }
    }
    //异常消息 添加
    public function abnormal_add()
    {
        if (!Request::instance()->isPost())
        {
            $this->error('请求方式错误');
        }


    }

}